package com.bw.dao.impl;

import com.bw.dao.PurchaseDao;
import com.bw.pojo.vo.PurchaseVo;

import java.math.BigDecimal;
import java.sql.Connection;
import java.util.List;

public class PurchaseDaoImpl extends BaseDao implements PurchaseDao {
    @Override
    public List<PurchaseVo> getPurchaseList(PurchaseVo purchaseVo) {
        String  sql = "select * from t_purchase where 1=1";
        if(null !=purchaseVo.getPurchaseCode() &&!purchaseVo.getPurchaseCode().isEmpty()){
            sql+= " and INSTR(purchase_code,'"+purchaseVo.getPurchaseCode()+"')";
        }
        sql += " limit ?,?";
        int index = (purchaseVo.getPageNum()-1)*purchaseVo.getPageSize();
        List<PurchaseVo> list = baseQueryList(PurchaseVo.class, sql, index,purchaseVo.getPageSize());
        return list;
    }

    @Override
    public int count(PurchaseVo purchaseVo) {
        String sql = "select count(1) from t_purchase where 1=1";
        if(null !=purchaseVo.getPurchaseCode() &&!purchaseVo.getPurchaseCode().isEmpty()){
            sql+= " and INSTR(purchase_code,'"+purchaseVo.getPurchaseCode()+"')";
        }
        Long aLong = baseQueryCount(Long.class, sql);
        return aLong.intValue();
    }

    /**
     * 给进货单据表 添加一条数据
     * @param conn
     * @param purchaseVo
     * @return
     */
    @Override
    public int addPurchase(Connection conn, PurchaseVo purchaseVo) {
        String sql = "insert into t_purchase(purchase_code,\n" +
                "purchase_time,\n" +
                "purchase_num,\n" +
                "purchase_sum_price,\n" +
                "address,\n" +
                "uid,\n" +
                "supplier_id,\n" +
                "discount,\n" +
                "pay_money) values (?,?,?,?,?,?,?,?,?)";
        //获取主键id
        Integer integer = baseInsertWithId(conn, sql, "purchase_id",
                purchaseVo.getPurchaseCode(),
                purchaseVo.getPurchaseTime(),
                purchaseVo.getPurchaseNum(),
                purchaseVo.getPurchaseSumPrice(),
                purchaseVo.getAddress(),
                purchaseVo.getUid(),
                purchaseVo.getSupplierId(),
                purchaseVo.getDiscount(),
                purchaseVo.getPayMoney());
        return integer;
    }

    /**
     * 给中间表添加测试数据
     * @param conn
     * @param purchaseId
     * @param goodsId
     * @param num
     * @param money
     */
    @Override
    public void addPurGoods(Connection conn, int purchaseId, Integer goodsId, Integer num, BigDecimal money) {
        String sql = "insert into t_purchase_goods(" +
                "purchase_id,\n" +
                "goods_id,\n" +
                "num,\n" +
                "money\n) values (?,?,?,?)";
        baseUpdate(conn,sql,purchaseId,goodsId,num,money);
    }

    /**
     * 更新库存
     * @param conn
     * @param num
     * @param goodsId
     */
    @Override
    public void updGoods(Connection conn, Integer num, Integer goodsId) {
        String sql = "update t_goods set inventory_quantity = inventory_quantity+? where goods_id = ?";
        baseUpdate(conn,sql,num,goodsId);
    }

    @Override
    public Integer addPurchases(Connection conn, PurchaseVo purchaseVo) {
        String sql = "insert into t_purchase(purchase_code,\n" +
                "purchase_time,\n" +
                "purchase_num,\n" +
                "purchase_sum_price,\n" +
                "address,\n" +
                "uid,\n" +
                "supplier_id,\n" +
                "discount,\n" +
                "pay_money) values (?,?,?,?,?,?,?,?,?)";
        //获取主键id
        Integer integer = baseInsertWithId(conn, sql, "purchase_id",
                purchaseVo.getPurchaseCode(),
                purchaseVo.getPurchaseTime(),
                purchaseVo.getPurchaseNum(),
                purchaseVo.getPurchaseSumPrice(),
                purchaseVo.getAddress(),
                purchaseVo.getUid(),
                purchaseVo.getSupplierId(),
                purchaseVo.getDiscount(),
                purchaseVo.getPayMoney());
        return integer;
    }

    @Override
    public void getAddPurchase(Connection conn, Integer purchaseId, Integer goodsId, Integer num, BigDecimal money) {
        String sql = "insert into t_purchase_goods(" +
                "purchase_id,\n" +
                "goods_id,\n" +
                "num,\n" +
                "money\n) values (?,?,?,?)";
        baseUpdate(conn,sql,purchaseId,goodsId,num,money);
    }

    @Override
    public void getUpdGoods(Connection conn, Integer num, Integer goodsId) {
        String sql = "update t_goods set inventory_quantity = inventory_quantity+? where goods_id = ?";
        baseUpdate(conn,sql,num,goodsId);
    }
}
